package com.CampusLife.controller;

import  com.CampusLife.bean.News;
import com.CampusLife.bean.Product;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import  com.CampusLife.service.INewsService;
import  com.CampusLife.until.DataTime;
import  com.CampusLife.until.ImagesUpload;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {
    @Resource
    private INewsService iNewsService;

    @RequestMapping("/queryAll")
    public String findall(Model model) {
        List<News> news = iNewsService.queryAll();
        model.addAttribute("news", news);
        return "admin/newbasic";
    }
    @RequestMapping("/query")
    public String queryAll(Model model,@RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo) {
        Integer pageSize=30;
        PageHelper.startPage(pageNo, pageSize);
        List<News> news = iNewsService.queryAll();
        PageInfo<News> pageInfo=new PageInfo<News>(news);
        model.addAttribute("pageInfo", pageInfo);
        return "home/newlist";
    }
    @RequestMapping("/jumpAddNews")
    public  String jumpAddNews(){
        return "admin/addnew";
    }

    @RequestMapping(path = "/addNew", method = RequestMethod.POST)
    public String addUser(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                          HttpServletResponse response, Model model, String title, String authority, String news_txt) throws Exception {
        ImagesUpload imagesUpload = new ImagesUpload();
        String photo = imagesUpload.Upload(file);
        News news = new News();
        news.setAuthority(authority);
        news.setNews_txt(news_txt);
        news.setPhoto(photo);
        news.setTitle(title);
        news.setViewNumber(0);
        DataTime dataTime = new DataTime();
        news.setTime(dataTime.CNtime());
        iNewsService.add(news);
        return "redirect:/news/queryAll";
    }
      //跳转到修改页面，回显数据
    @RequestMapping("/jumpUpdateNewsEdit")
    public String jumpUpdateUserEdit(Integer nid, Model model) {

        News news = iNewsService.queryById(nid);
        model.addAttribute("news", news);
        return "admin/updateNew";
    }

    /**
     * 修改通告内容
     * @param file
     * @param request
     * @param response
     * @param model
     * @param title
     * @param authority
     * @param news_txt
     * @return
     * @throws Exception
     */
    @RequestMapping(path = "/updateNews", method = RequestMethod.POST)
    public String updateNews(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                             HttpServletResponse response, Model model, String title, String authority, String news_txt) throws Exception {
        ImagesUpload imagesUpload = new ImagesUpload();
        String photo = imagesUpload.Upload(file);
        News news = new News();
        news.setAuthority(authority);
        news.setNews_txt(news_txt);
        news.setPhoto(photo);
        news.setTitle(title);
        DataTime dataTime = new DataTime();
        news.setTime(dataTime.CNtime());
        iNewsService.update(news);
        return "redirect:/news/queryAll";
    }

    @RequestMapping("/deleteNews")
    public String deleteUser(Integer nid) {

        iNewsService.delete(nid);
        return "redirect:/news/queryAll";
    }
    @RequestMapping("/queryByTitle")
    public String queryByTitle(String title,Model model) {

      List<News> news= iNewsService.queryByTitle(title);
        model.addAttribute("news", news);
        return "admin/newbasic";
    }
    @RequestMapping("/queryByTitle1")
    public String queryByTitle1(String title,Model model) {

        List<News> news= iNewsService.queryByTitle(title);
        model.addAttribute("news", news);
        return "home/newlist";
    }

    @RequestMapping("/getOrder")
    public String getOrder(Model model) {
        List<News> news= iNewsService.getOrder();
        model.addAttribute("news", news);
        return "home/home";
    }
    @RequestMapping("Byid")
    public  String Byid(Integer nid,Model model){
     News news=  iNewsService.queryById(nid) ;
    int i= iNewsService.queryViewNumber(nid);
        int m=i+1;
        News news1=new News();
        news1.setNid(nid);
        news1.setViewNumber(m);
       iNewsService.updateviewNumber(news1);
     model.addAttribute("news",news);
     return "home/new";
    }

}
